package com.example.sup.conttroller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.sup.aspect.annotation.CheckLogin;
import com.example.sup.conttroller.request.TaskManagementAddRequest;
import com.example.sup.conttroller.request.TaskManagementDeleteRequest;
import com.example.sup.conttroller.request.TaskManagementEditRequest;
import com.example.sup.conttroller.request.TaskManagementRequest;
import com.example.sup.conttroller.response.ServerResponse;
import com.example.sup.entity.TaskManagement;
import com.example.sup.entity.UserInfo;
import com.example.sup.service.TaskManagementService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @ClassName TaskManagementController
 * @Description {TODO}
 * @Author 李哲
 * @Date 9:29 2024/7/24
 * @Version 1.0
 */
@RestController
@Slf4j
@RequestMapping("/taskmanagement")
@Api(description = "定时任务控制器")
@CrossOrigin(allowCredentials = "true", allowedHeaders = "*")
public class TaskManagementController {

    @Resource
    private TaskManagementService taskManagementService;

    @PostMapping("/query")
    public ServerResponse query(@RequestBody TaskManagementRequest taskManagementRequest){
        IPage<TaskManagement> page = new Page<>(taskManagementRequest.getCurrent(), taskManagementRequest.getSize());

        if (!taskManagementRequest.getTaskName().isEmpty()){
            QueryWrapper<TaskManagement> wrapper = new QueryWrapper<>();
            wrapper.like("task_name",taskManagementRequest.getTaskName());
            return ServerResponse.successWithMsg("操作成功",page.setRecords(taskManagementService.list(page,wrapper)));
        }
        if (!taskManagementRequest.getPlanSettingName().isEmpty()){
            QueryWrapper<TaskManagement> wrapper = new QueryWrapper<>();
            wrapper.like("plan_setting_name",taskManagementRequest.getPlanSettingName());
            return ServerResponse.successWithMsg("操作成功",page.setRecords(taskManagementService.list(page,wrapper)));
        }
        List<TaskManagement> taskManagements = taskManagementService.list(page);
        page.setRecords(taskManagements);
        page.setTotal(taskManagementService.count());
        page.setSize(taskManagementRequest.getSize());



        return ServerResponse.successWithMsg("操作成功",page);
    }


    @PostMapping("/add")
    public ServerResponse add(@RequestBody TaskManagementAddRequest request){
        TaskManagement taskManagement = new TaskManagement();
        BeanUtils.copyProperties(request, taskManagement);
        taskManagementService.save(taskManagement);
        return ServerResponse.successWithMsg("操作成功",null);
    }

    @PostMapping("/delete")
    public ServerResponse delete(@RequestBody TaskManagementDeleteRequest request){
        boolean result = taskManagementService.removeById(request.getTaskId());
        if (result){
            return ServerResponse.successWithMsg("操作成功",null);
        }
        return ServerResponse.successWithMsg("操作失败",null);
    }

    @PostMapping("/update")
    @ApiOperation(value = "更新定时任务",tags = "更新定时任务")
    @CheckLogin
    public ServerResponse update(UserInfo userInfo ,
                                 @RequestBody @Validated TaskManagement taskManagement,
                                 HttpServletRequest request){
        //TODO 1.参数校验--
        log.info("登录的用户信息:{}",userInfo);
        //TODO 2.调用service层进行更新业务操作
        return taskManagementService.updateByTaskId(taskManagement,userInfo);
    }

}
